package com.battery.web.controller.system.battery;

import java.math.BigDecimal;
import java.util.List;

import com.battery.system.domain.BO.MerchantBO;
import com.battery.system.domain.MerchantInstall;
import com.battery.system.domain.Partner;
import com.battery.system.domain.PartnerInstall;
import com.battery.system.domain.VO.MerchantExcelVO;
import com.battery.system.domain.VO.MerchantVO;
import com.battery.system.service.battery.IMerchantInstallService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.battery.common.annotation.Log;
import com.battery.common.enums.BusinessType;
import com.battery.system.domain.Merchant;
import com.battery.system.service.battery.IMerchantService;
import com.battery.common.core.controller.BaseController;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.utils.poi.ExcelUtil;
import com.battery.common.core.page.TableDataInfo;

/**
 * 商户Controller
 *
 * @author battery
 * @date 2021-01-22
 */
@Controller
@RequestMapping("/system/merchant")
public class MerchantController extends BaseController {
    private String prefix = "system/merchant";

    @Autowired
    private IMerchantService merchantService;

    @Autowired
    private IMerchantInstallService merchantInstallService;


    @RequiresPermissions("system:merchant:view")
    @GetMapping()
    public String merchant() {
        return prefix + "/merchant";
    }

    /**
     * 查询商户列表
     */
    @RequiresPermissions("system:merchant:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MerchantBO merchantBO) {
        startPage();
//        List<Merchant> list = merchantService.selectMerchantList(merchant);
        List<MerchantVO> list = merchantService.selectMerchantVOList(merchantBO);
        return getDataTable(list);
    }

    /**
     * 导出商户-门店数据列表 -- 暂时不用
     */
    @RequiresPermissions("system:merchant:export")
    @Log(title = "商户门店", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MerchantBO merchantBO) {
//        List<Merchant> list = merchantService.selectMerchantList(merchant);
        List<MerchantExcelVO> list = merchantService.selectMerchantExcelVOList(merchantBO);
        ExcelUtil<MerchantExcelVO> util = new ExcelUtil<MerchantExcelVO>(MerchantExcelVO.class);
        return util.exportExcel(list, "merchant");
    }

    /**
     * 新增商户
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商户
     */
    @RequiresPermissions("system:merchant:add")
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Merchant merchant) {
        return toAjax(merchantService.insertMerchant(merchant));
    }

    /**
     * 修改商户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Merchant merchant = merchantService.selectMerchantById(id);
        mmap.put("merchant", merchant);
        return prefix + "/edit";
    }

    /**
     * 修改保存商户
     */
    @RequiresPermissions("system:merchant:edit")
    @Log(title = "商户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Merchant merchant) {
        return toAjax(merchantService.updateMerchant(merchant));
    }

    /**
     * 删除商户
     */
    @RequiresPermissions("system:merchant:remove")
    @Log(title = "商户", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(merchantService.deleteMerchantByIds(ids));
    }


    /**
     * 跳转到设置页面
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/putUpBase/{id}")
    public String putUpBase(@PathVariable("id") String id, ModelMap mmap) {
        MerchantInstall merchantInstall = merchantInstallService.selectByPrimaryKey(id);
        Merchant merchant = merchantService.selectByPrimaryKey(id);
        MerchantBO merchantBO = new MerchantBO();
        merchantBO.setFrozenMoney(merchant.getFrozenMoney());
        merchantBO.setCashoutMoney(merchant.getCashoutMoney());

        merchantBO.setMerchantId(id);
        merchantBO.setTxBrokerage(merchantInstall.getTxBrokerage());
        merchantBO.setTxMinPermit(merchantInstall.getTxMinPermit());
        merchantBO.setDeatilAbility(merchantInstall.getDeatilAbility());
        merchantBO.setCashoutAbility(merchantInstall.getCashoutAbility());
        mmap.put("merchantInstall", merchantBO);
        return prefix + "/putUpBase";
    }

    @RequiresPermissions("system:merchant:putUpBase")
    @Log(title = "商户", businessType = BusinessType.UPDATE)
    @PostMapping("/putUpBase")
    @ResponseBody
    public AjaxResult putUpBase(MerchantBO merchantInstall) {
        if (merchantInstall.getMerchantId() == null) {
            return error("商户ID为空");
        }
        if (merchantInstall.getTxBrokerage() == null) {
            return error("请填写提现手续费！");
        }

        if (merchantInstall.getTxBrokerage().compareTo(BigDecimal.ZERO) < 0 || merchantInstall.getTxBrokerage().subtract(new BigDecimal(100)).compareTo(BigDecimal.ZERO) > 0) {
            return error("提现手续费不能小于0，且不能大于100");
        }

        if (merchantInstall.getTxMinPermit() == null) {
            return error("请填写提现允许最小金额！");
        }

        if (merchantInstall.getTxMinPermit().compareTo(BigDecimal.ZERO) < 0) {
            return error("提现允许最小金额不能小于0");
        }

        if (merchantInstall.getDeatilAbility() == null) {
            return error("请选择‘账单明细功能’");
        }

        if (merchantInstall.getCashoutAbility() == null) {
            return error("请选择‘商户提现功能’");
        }

        // 查询冻结金额
        if (merchantInstall.getFrozenMoney() == null) {
            return error("冻结金额不能为空");
        }
        if (merchantInstall.getFrozenMoney().compareTo(BigDecimal.ZERO) < 0) {
            return error("冻结金额不能小于0");
        }
        if (merchantInstall.getCashoutMoney() == null) {
            return error("可提现金额不能为空");
        }

        if (merchantInstall.getCashoutMoney().compareTo(BigDecimal.ZERO) < 0) {
            return error("可提现金额不能小于0");
        }

        return toAjax(merchantInstallService.setUpMerchant(merchantInstall));
    }


    /**
     * 查看详情
     */
    @GetMapping("/seeDetail/{id}")
    public String seeDetail(@PathVariable("id") String id, ModelMap mmap) {
        Merchant merchant = merchantService.selectByPrimaryKey(id);
        mmap.put("merchant", merchant);
        return prefix + "/seeDetail";
    }


}
